package org.hang.gift.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    //注册成功，发送邮件
    public void registerSuccess(String to, String subject) {
        try {
            //创建复杂的邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类，第二个参数为 是否创建多个部分组成的消息 第三个参数为编码集
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            //1.设置发件人
            helper.setFrom("1371608442@qq.com");
            //2，设置收件人
            helper.setTo(to);
            //3，设置主题
            helper.setSubject(subject);
            helper.setText( "<h3>您的店铺注册成功，正在审核中，请耐心等待审核结果</h3>\n",true);
            //helper.setText(text,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }



    //审核成功，发送激活邮件
    public void censorSuccess(String to, String subject, Long tenantId) {
        try {
            //创建复杂的邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类，第二个参数为 是否创建多个部分组成的消息 第三个参数为编码集
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            //1.设置发件人
            helper.setFrom("1371608442@qq.com");
            //2，设置收件人
            helper.setTo(to);
            //3，设置主题
            helper.setSubject(subject);
            String uuid = UUID.randomUUID().toString();
            //将tenantId加上前缀"tenant_active_"作为键 和 uuid作为值存入Redis 设置过期时间为30分钟
            redisTemplate.opsForValue().set("tenant_active_" + tenantId, uuid, 30, TimeUnit.MINUTES);
            //拼接 超链接，邮件作为前端，将shopId 和 uuid 发到controller进行激活
            //String text = "<a href='http://127.0.0.1:8080/shop/active?shopId=" + shopId + "&activeCode=" + uuid + "'>您的店铺审核通过，点击这里激活店铺</a>";
            helper.setText( "<h3>您的店铺审核已经通过，点击下方链接即可激活店铺</h3>\n" +
                    "<a href='http://localhost:1010/apis/system/tenant/active?tenantId=" + tenantId + "&activeCode=" + uuid + "'>点击这里激活店铺</a>",true);
            //helper.setText(text,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    //审核驳回，发送通知邮件
    public void censorFail(String to, String subject, String note) {
        try {
            //创建复杂的邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类，第二个参数为 是否创建多个部分组成的消息 第三个参数为编码集
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            //1.设置发件人
            helper.setFrom("1371608442@qq.com");
            //2，设置收件人
            helper.setTo(to);
            //3，设置主题
            helper.setSubject(subject);
            //4，邮件内容
            helper.setText("<h3>您申请的店铺审核未通过！驳回原因为:</h3>" +note+
                    "<h3>您可以点击下方链接重新提交审核资料</h3>\n" +
                    "    <a href=\"http://127.0.0.1:8081/#/register\">\n"+
                    "    <h3>重新提交店铺资料</h3>\n" +
                    "    </a>",true);

            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }




}

